package com.innerchic.weqiuqiu.service;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.Service;
import android.app.UiModeManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.innerchic.mapper.log.Logger;
import com.innerchic.weqiuqiu.R;
import com.innerchic.weqiuqiu.assembly.MessageListView;
import com.innerchic.weqiuqiu.ui.ContentActivity;
import com.innerchic.weqiuqiu.utils.NotificationUtils;

/**
 * ================================================
 * 作    者：贾恒飞 >>> 17515250730@163.com
 * 项    目：wedudu
 * 日    期：2022/1/13 11:30
 * 包    名：com.innerchic.weqiuqiu.service
 * 描    述：球球的Service
 * Create by zFox from AndroidStudio4.2
 * ================================================
 */
public class QiuQiuService extends Service {

    /**
     * 通知ID
     */
    private static final int NOTIFICATION_ID = 333;

    /**
     * 启动当前服务
     *
     * @param context      上下文
     * @param openEntrance 启动入口
     */
    public static void startQiuQiuService(Context context, String openEntrance) {
        Intent serviceIntent = new Intent(context, QiuQiuService.class);
        serviceIntent.putExtra("open_entrance", "BootMonitorReceiver");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(serviceIntent);
        } else {
            context.startService(serviceIntent);
        }
    }

    private MessageListView messageListView;

    @SuppressLint("WrongConstant")
    @Override
    public void onCreate() {
        super.onCreate();
        Logger.d("Service启动...");
        //显示服务通知
        Notification notification = NotificationUtils.createForegroundNotification(this, getResources().getString(R.string.app_name),
                "消息服务正在运行中...", new Intent(this, ContentActivity.class));
        startForeground(NOTIFICATION_ID, notification);
        //开启消息接收器
        boolean listenersEnabled = NotificationUtils.isNotificationListenersEnabled(this);
        if (listenersEnabled) {
            NotificationUtils.toggleNotificationListenerService(this, MessageCoreService.class);
        }
        //开启驾车模式,方便接收微信消息
        boolean isOpenCarMode = ((UiModeManager) getSystemService("uimode")).getCurrentModeType() == Configuration.UI_MODE_TYPE_CAR;
        if (!isOpenCarMode) {
            UiModeManager uimode = (UiModeManager) getSystemService("uimode");
            uimode.enableCarMode(0);
        }
        //初始化悬浮窗组件
        messageListView = new MessageListView();
        messageListView.create(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Logger.d("Service On Start Command ...");
        //检查并开启消息接收器
        boolean listenersEnabled = NotificationUtils.isNotificationListenersEnabled(this);
        if (listenersEnabled) {
            NotificationUtils.toggleNotificationListenerService(this, MessageCoreService.class);
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @SuppressLint("WrongConstant")
    @Override
    public void onDestroy() {
        stopForeground(true);
        messageListView.destroy();
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}
